package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 山外小王
 * @Description 检查组Controller
 * @Date 2024/04/13/16:36
 */
@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("[检查组-新增]data:{},checkitemIds{}",checkGroup,checkitemIds);
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS,null);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroups);
    }
    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[检查组-分页查询]data:{}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Long id){
        log.info("[检查组-删除]id:{}",id);
        return checkGroupService.deleteCheckGroupitemById(id);
    }

    /**
     * 回显检查组数据
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId (Long id){
        log.info("[检查组-回显数据]id:{}",id);
        return checkGroupService.findCheckItemIdsByCheckGroupId(id);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById (Long id){
        log.info("[检查组-根据id查询检查组]id:{}",id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroupService.findById(id));
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[检查组-编辑检查组]data{},id:{}",checkGroup,checkitemIds);
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS,null);
    }

    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        Integer[] checkGroupIds = checkGroupService.findCheckGroupIdsBySetmealId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroupIds);
    }
}
